package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;

import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.utils.Hermite;

import java.util.ArrayList;

/**
 * Created by jw.gu on 2016/6/13.
 */
public abstract class AbstractMapView extends RelativeLayout {
    public static final int MIN_PX = 80;
    public static final int FENCE_MAX_MAP_ZOOM = 12;

    public static enum MapType {NORMAL, SATELLITE, NIGHT}

    ;

    public static interface InitCompleteListener {
        public void onInitComplete();
    }

    protected Context context;
    protected View mView;
    protected MapType mapType = MapType.NORMAL;
    public static int unFlyedRouteColor = Color.parseColor("#4bd56f");
    protected int flyedRouteColor = Color.parseColor("#999999");
    protected int cirlceStrokeColor = Color.parseColor("#239DDA");
    protected int cirlceFillColor = Color.parseColor("#33239DDA");
    protected int routeWidth = 8;
    protected int cirlceWidth = 2;
    protected int droneMinScreenX = 210;
    protected int droneMaxScreenX = 1150;
    protected int droneMinScreenY = 190;
    protected int droneMaxScreenY = 580;
    protected InitCompleteListener initCompleteListener;

    public void setInitCompleteListener(InitCompleteListener initCompleteListener) {
        this.initCompleteListener = initCompleteListener;
    }

    public MapType getMapType() {
        return mapType;
    }

    public void setMapType(MapType mapType) {
        if (mapType != this.mapType) {
            this.mapType = mapType;
            onMapTypeChanged();
        }
    }

    public AbstractMapView(Context contextParam) {
        super(contextParam);
        context = contextParam;
    }

    /**
     * 当地图类型从标准、卫星、夜间之间切换时调用
     */
    protected abstract void onMapTypeChanged();

    /**
     * 删除指定序号航点的标志
     *
     * @param index
     */
    public abstract void onPointDelete(int index);

    /**
     * 删除指定序号兴趣点的标志
     *
     * @param index
     */
    public abstract void onInterestPointDelete(int index);

    /**
     * 移除地图长按点的标志
     */
    public abstract void removeLongClickMarker();

    /**
     * 移除降落点的标志
     */
    public abstract void removeLandPointMarker();

    /**
     * 添加一个指定序号的航点到指定位置
     *
     * @param index
     * @param latitude
     * @param longitude
     */
    public abstract void insertOnePoint(int index, double latitude, double longitude);

    /**
     * 根据航点绕圈半径画出圆
     *
     * @param index
     * @param radius
     */
    public abstract void addPointCircle(int index, int radius);

    /**
     * 根据航点yaw值转动航点图标
     *
     * @param index
     * @param targetAngle
     */
    public abstract void changeMarkerRotateAngle(int index, float targetAngle);

    /**
     * 设置飞机标志
     *
     * @param latitude
     * @param longitude
     * @param angle     云台角度
     */
    public abstract void drawDroneMarker(double latitude, double longitude, float angle);


    public abstract void setPanAngle(int angle);

    /**
     * 设置遥控器标志
     *
     * @param latitude
     * @param longitude
     */
    public abstract void drawControllerMarker(double latitude, double longitude);

    /**
     * 退出航点模式
     */
    public abstract void exitPointType();

    /**
     * 将地图中心移动到飞机所在位置
     */
    public abstract void moveCameraToDroneLocation();

    /**
     * 将地图中心移动到遥控器所在位置
     */
    public abstract void moveCameraToControllerLocation();

    /**
     * 在设置飞机位置的时候调用，当飞机飞到地图边缘一定距离的时候，将地图中心移动到飞机所在位置
     */
    public abstract void checkMoveCamera();

    /**
     * 设置当前已经飞过的点
     *
     * @param index
     */
    public abstract void setCurrentWaypointIndex(int index);

//    /**
//     * 载入已保存的航线
//     * @param waypoints
//     */
//    public abstract void loadPoints(ArrayList<Waypoint> waypoints);
//
//    /**
//     * 载入已保存的航线的兴趣点
//     * @param interestPoints
//     */
//    public abstract void loadInterestPoints(ArrayList<InterestPoint> interestPoints);

    /**
     * 修改航线类型
     *
     * @param routeType
     */
    public abstract void changeRouteType(byte routeType);

    public abstract void setPointMarkerDraggable(boolean draggable);

    public abstract void setLandPointMarkerDraggable(boolean draggable);

    public abstract void setFenceMarkerDraggable(boolean draggable);

    public abstract void onPause();

    public abstract void onResume();

    public abstract void onDestroy();

    public abstract void clearMap();

    public abstract void checkFenceMapZoom();


    public static class Vector2 {
        public double x;
        public double y;

        public Vector2() {
        }

        public Vector2(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }

    protected ArrayList<Vector2> getCurveRouteVector2s(int startIndex, int endIndex) {//index start from 1
        return Hermite.getCurveRouteVector2s(startIndex, endIndex);
//        return getStraightRouteVector2s(startIndex,endIndex);
    }

//    protected ArrayList<Vector2> getStraightRouteVector2s(int startIndex,int endIndex) {//index start from 1
//        ArrayList<Vector2> result=new ArrayList<>();
//        for (int i = startIndex-1; i <endIndex ; i++) {
//            result.add(new Vector2(GlobalParams.waypoints.get(i).latitude,GlobalParams.waypoints.get(i).longitude));
//        }
//        return result;
//    }
}
